/*** 
This do-file calculates numbers for the paper based on Womply ZIP-level
data.
***/

*-------------------------------------------------------------------------------
* Set up 
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Small Business Revenue"

* Create required subfolders
cap mkdir "${root}/results/Small Business Revenue"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml"

*-------------------------------------------------------------------------------
* Load and process data 
*-------------------------------------------------------------------------------

project, uses("${root}/data/web/data/Womply - ZCTA - 2020.csv") 
import delimited  "${root}/data/web/data/Womply - ZCTA - 2020.csv", clear

* Convert the changes to percentages
replace revenue_all_apr2020 = revenue_all_apr2020 * 100
replace revenue_all_jul2020 = revenue_all_jul2020 * 100

* Merge ZCTA-level covariates
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
merge 1:1 zcta using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", ///
	keep(1 3) nogen keepusing(pop_2014_2018_est medhhinc_2014_2018_est med_2br_2014_2018_est zarealand)
	
gen zarealand_sqm = zarealand/ (1609.37 ^ 2)
drop zarealand
rename (pop_2014_2018_est medhhinc_2014_2018_est med_2br_2014_2018_est) (pop_2018 medhhinc_2018 med_2br_2018)

* Gen population density by ZCTA
gen pop_conc_zcta = pop_2018 / zarealand_sqm

* Put population density on a log scale
gen l_pop_conc_zcta = log(pop_conc_zcta) 

* Merge counties (for SEs)
rename zcta zcta5
preserve
	project, uses("${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt")
	import delimited "${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt", encoding(ISO-8859-2) clear
	sort zcta5 poppt
	bysort zcta5 (poppt): keep if _n == _N // only keeps the county where most of the zcta lives
	gen county_fips = 1000 * state + county
	keep zcta5 county_fips
	tempfile zcta_county_CW
	save `zcta_county_CW'
restore

merge 1:1 zcta5 using `zcta_county_CW', keep(1 3 ) nogen
rename zcta5 zcta 

reshape long revenue_all_ , i(zcta county_fips pop_2018 med_2br_2018) j(period) string

* Run regressions 
reg revenue_all_ med_2br_2018 [w = pop_2018] if period == "apr2020", vce(cluster county_fips)
local display_beta_april : di %4.2f _b[med_2br_2018] * 1000 
local display_se_april : di %4.2f _se[med_2br_2018] * 1000 

reg revenue_all_ med_2br_2018 [w = pop_2018] if period == "jul2020", vce(cluster county_fips)
local display_beta_july : di %4.2f _b[med_2br_2018] * 1000 
local display_se_july : di %4.2f _se[med_2br_2018] * 1000 

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

binscatter revenue_all_ med_2br_2018 ///
	[w = pop_2018] ///
	if inlist(period, "apr2020", "jul2020") /// 
	, ///
	by(period) /// 
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	xlab(500(500)2000) /// 
	ytitle("Change in Small Business Revenue (%)" "Relative to January 2020") ///
	msymbols(circle diamond) /// 
	colors(oi1 oi2) /// 
	ylab(-60 "-60%" -40 "-40%" -20 "-20%" 0 "0%" 20 "20%", nogrid) /// 
	${title_`version'} ///
	legend(off) /// 
	text(-40 2000 "Change from Jan-Apr" "Slope = `display_beta_april'%/$1000" "(s.e. = `display_se_april')",  size(medsmall) color(oi1) j(left)) /// 
	text(10 2000 "Change from Jan-Jul" "Slope = `display_beta_july'%/$1000" "(s.e. = `display_se_july')", size(medsmall) color(oi2) j(left))  	
oi_graph_export "${root}/results/Small Business Revenue/Changes in small business revenue vs median rent by ZIP", type(${fig_type})

* Export output numbers to csv file
yamlout using "${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml", ///
	key("jan_apr_slope") ///
	comment("Jan-Apr Slope (%/$1000)") ///
	value(`display_beta_april') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml", ///
	key("jan_apr_se") ///
	comment("Jan-Apr SE") ///
	value(`display_se_april') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml", ///
	key("jan_jul_slope") ///
	comment("Jan-Jul Slope (%/$1000)") ///
	value(`display_beta_july') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml", ///
	key("jan_jul_se") ///
	comment("Jan-Jul SE") ///
	value(`display_se_july') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Change in Small Business Revenue vs. Median Rent, by ZIP Appendix.yaml")
